package com.gperp.controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gperp.common.ChanpinfenleiBaobiao;
import com.gperp.common.ExcelUtil;
import com.gperp.common.TimeResult;
import com.gperp.service.BaobiaoService;

/**
 * 控制器:将数据导出到excel表格中
 * 
 * @author yangming
 */
@Controller
public class ChanpinfenleiDownController {
	
	@Autowired
	private BaobiaoService baobiaoService;
	
	@RequestMapping(value = "chanpenfenleidown")
	@CrossOrigin
	public String download(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		//设置文件名称
		String fileName = "ChanpinfenleiBaobiao数据";
		
		// 填充ChanpinfenleiBaobiaos数据
		List<ChanpinfenleiBaobiao> ChanpinfenleiBaobiaos = createData();
		//创建表格所需要的数据
		List<Map<String, Object>> list = createExcelRecord(ChanpinfenleiBaobiaos);
		//标题行名称
		String columnNames[] = { "id", "产品分类名称", "数量","价格"};// 列名
		//需要和指定的key对应
		String keys[] = { "id", "configTypeName", "nums","countMoney"};// map中的key
		
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			ExcelUtil.createWorkBook(list, keys, columnNames).write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		
		
		// 设置response参数，可以打开下载页面
		response.reset();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
		
		ServletOutputStream out = response.getOutputStream();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			// Simple read/write loop.
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (final IOException e) {
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
		return null;
	}


	/**
	 * 模拟数据库查询数据
	 * @return
	 */
	private List<ChanpinfenleiBaobiao> createData() {
		List<ChanpinfenleiBaobiao> ChanpinfenleiBaobiaoList = baobiaoService.getchanpinfenleiDown();
		return ChanpinfenleiBaobiaoList;
	}

	/**
	 * 将List中的对象转换成Map
	 * @param ChanpinfenleiBaobiaos
	 * @return
	 */
	private List<Map<String, Object>> createExcelRecord(List<ChanpinfenleiBaobiao> ChanpinfenleiBaobiaos) {
		List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		//设置数据也的名称
		map.put("sheetName", "项目数据");
		listmap.add(map);
		ChanpinfenleiBaobiao ChanpinfenleiBaobiao = null;
		for (int j = 0; j < ChanpinfenleiBaobiaos.size(); j++) {
			ChanpinfenleiBaobiao = ChanpinfenleiBaobiaos.get(j);
			Map<String, Object> mapValue = new HashMap<String, Object>();
			mapValue.put("id", ChanpinfenleiBaobiao.getId());
			mapValue.put("configTypeName", ChanpinfenleiBaobiao.getConfigTypeName());
			mapValue.put("nums", ChanpinfenleiBaobiao.getNums());
			mapValue.put("countMoney", ChanpinfenleiBaobiao.getCountMoney());
			listmap.add(mapValue);
		}
		return listmap;
	}
}
